package ia.robot.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

/**
 * Clase que representa el dialogo de creacion de tableros.
 * 
 * @author Jaime A. Corrales Gonzalez
 * @author Alejandro Tejera Perez
 */
public class NewDialog extends JDialog implements ActionListener {

	private static final long serialVersionUID = 1L;

	private final String acceptString = "Accept";
	private final String cancelString = "Cancel";

	private Dimension d;

	private JButton acceptButton;
	private JButton cancelButton;

	private JTextField widthTF;
	private JTextField heightTF;

	/**
	 * Constructor dado el marco poseedor del dialogo.
	 * 
	 * @param owner
	 *            - Marco poseedor del dialogo.
	 */
	public NewDialog(JFrame owner) {
		super(owner, "New", true);

		acceptButton = new JButton(acceptString);
		acceptButton.addActionListener(this);
		cancelButton = new JButton(cancelString);
		cancelButton.addActionListener(this);

		widthTF = new JTextField("10", 10);
		heightTF = new JTextField("10", 10);

		JLabel widthLabel = new JLabel("Width: ");
		JLabel heightLabel = new JLabel("Height: ");

		JPanel centerPane = new JPanel(new GridLayout(0, 2, 0, 5));
		centerPane.setBorder(new EmptyBorder(5, 5, 0, 5));
		centerPane.add(widthLabel);
		centerPane.add(widthTF);
		centerPane.add(heightLabel);
		centerPane.add(heightTF);

		JPanel lowerPane = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 5));
		lowerPane.setBorder(new EmptyBorder(0, 5, 0, 5));
		lowerPane.add(acceptButton);
		lowerPane.add(cancelButton);

		getContentPane().setLayout(new BorderLayout());
		this.getContentPane().add(centerPane, BorderLayout.CENTER);
		this.getContentPane().add(lowerPane, BorderLayout.SOUTH);

		this.pack();
		this.setLocationRelativeTo(owner);
		this.setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {
		String actionCommand = e.getActionCommand();

		// Si el evento fue lanzado por un boton
		if (e.getSource() instanceof JButton) {
			// Si el evento fue lanzado por el boton Add Timestamp
			if (actionCommand.equals(acceptString)) {
				boolean error = false;
				int width = 0;
				int height = 0;

				try {
					width = Integer.parseInt(widthTF.getText());
					widthTF.setBackground(Color.WHITE);
				} catch (NumberFormatException ex) {
					widthTF.setBackground(Color.RED);
					error = true;
				}

				if (width <= 0) {
					widthTF.setBackground(Color.RED);
					error = true;
				}

				try {
					height = Integer.parseInt(heightTF.getText());
					heightTF.setBackground(Color.WHITE);
				} catch (NumberFormatException ex) {
					heightTF.setBackground(Color.RED);
					error = true;
				}

				if (height <= 0) {
					heightTF.setBackground(Color.RED);
					error = true;
				}

				if (!error) {
					d = new Dimension(width, height);

					this.setVisible(false);
				}
			}

			else if (actionCommand.equals(cancelString)) {
				setVisible(false);
			}
		}
	}

	/**
	 * Retorna las dimensiones del nuevo tablero.
	 * 
	 * @return Dimensiones del nuevo tablero.
	 */
	public Dimension getDimension() {
		return d;
	}
}
